package com.motus.pm.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.motus.pm.service.EmpService;
import com.motus.pm.vo.PmDeptTask;
import com.motus.pm.vo.PmProjectPhase;
import com.motus.pm.vo.PmProjectTask;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
public class EmpAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	@Qualifier("empServiceImpl")
	private EmpService empService;

	private List<PmProjectTask> taskList = new ArrayList<PmProjectTask>();
	private List<PmDeptTask> deptTaskList = new ArrayList<PmDeptTask>();
	private int userId; // 用id
	private String name;
	private PmProjectPhase projectPhase = new PmProjectPhase();
	private int taskId;
	private int state;
	PmProjectTask taskById = new PmProjectTask();

	public List<PmDeptTask> getDeptTaskList() {
		return deptTaskList;
	}

	public void setDeptTaskList(List<PmDeptTask> deptTaskList) {
		this.deptTaskList = deptTaskList;
	}

	public PmProjectTask getTaskById() {
		return taskById;
	}

	public void setTaskById(PmProjectTask taskById) {
		this.taskById = taskById;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public int getTaskId() {
		return taskId;
	}

	public void setTaskId(int taskId) {
		this.taskId = taskId;
	}

	public PmProjectPhase getProjectPhase() {
		return projectPhase;
	}

	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<PmProjectTask> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<PmProjectTask> taskList) {
		this.taskList = taskList;
	}

	//获取钉钉登陆用户名
	@Override
	public String execute() throws Exception {
//		if(name==null){
//			return "error";
//		}else{
//			userId=empService.getEmpInfoByName(name).getId();
//			int deptId=empService.getDeptIdByUserId(userId);
//			ActionContext ac=ActionContext.getContext();
//			Map<String, Object> session=ac.getSession();
//			session.put("userId", userId);
//			session.put("deptId", deptId);
//		}
		return super.execute();
	}
	/**
	 * 获取全部的任务信息
	 * 
	 * @return
	 */
	public String taskByState() {
//		userId = 60;
		ActionContext ac=ActionContext.getContext();
		Map<String, Object> session=ac.getSession();
		if(session.containsKey("userId")){
			userId=(int) session.get("userId");
			taskList = empService.tasksByState(state, userId);
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	public String taskById() {
		ActionContext ac=ActionContext.getContext();
		Map<String, Object> session=ac.getSession();
		userId=(int) session.get("userId");
		if (taskId == 0) {
			List<PmProjectTask> taskByUId = empService.tasksByState(0, userId); // 此时0用来获取最新的项目
			if (taskByUId.size() == 0) { // 当前没有待做项目
				return ERROR;
			} else {
				taskById = taskByUId.get(0);
			}
		} else {
			taskById = empService.taskById(taskId);
		}
		return SUCCESS;
	}

	/**
	 * 更新任务状态 提交任务，审核任务
	 * 
	 * @return
	 */
	public String changeTaskState(){
		PmProjectTask task = new PmProjectTask();
		task = empService.taskById(taskId);
		task.setTaskState(state);
		Date now = new Date();// new Date()为获取当前系统时间
		Timestamp oppTime = new Timestamp(now.getTime());
		if (state == 0) { // 0 :开始任务，将状态转换成0
			task.setTaskRealStartTime(oppTime);
		}
		if (state == 1) { // 1 ：提交任务，将状态转成1
			task.setTaskRealEndTime(oppTime);
		}
		empService.changeTaskState(task);
		return SUCCESS;
	}

	// 获取前序任务
	public String aheadTask() {
		System.out.println(taskId);
		projectPhase = empService.aheadTask(taskId);
		if (projectPhase == null) {
			return ERROR;
		}
		return SUCCESS;
	}

	// 会议列表
	public String showMeetingList() {
		ActionContext ac=ActionContext.getContext();
		Map<String, Object> session=ac.getSession();
		int deptId=(int) session.get("deptId");
		// 说明，deptid通过获取当前用户的信息来获取，type:通过角色权限判断判断
		deptTaskList = empService.getDeptTaskByMeetingTypeAndDeptId(4, deptId);
		return SUCCESS;
	}

	// 任务列表
	public String showTaskList() {
		// 根据任务的类型显示，初始化的显示进行中的任务
		int position = 0;
//		int userId = 60;
		ActionContext ac=ActionContext.getContext();
		Map<String, Object> session=ac.getSession();
		userId=(int) session.get("userId");
		taskList = empService.tasksByState(position, userId);
		return SUCCESS;
	}

	// 任务详情
	public String showTaskDetail() {
		return SUCCESS;
	}

	// 首页
	public String showEmpHome() {
		return SUCCESS;
	}
}
